import React, { Component } from 'react'

export default class Count extends Component {
    state = {
        sum: 0
    }
    increment=()=>{
        const {value} = this.selectNumbers
        const {sum} = this.state
        this.setState({
            sum:parseInt(sum)+parseInt(value)
        })
    }
    decrement=()=>{
        const {value} = this.selectNumbers
        const {sum} = this.state
        this.setState({
            sum:parseInt(sum)-parseInt(value)
        })
    }
    incrementifOdd=()=>{
        const {value} = this.selectNumbers
        const {sum} = this.state
        if(parseInt(sum)%2!==0){
            this.setState({
                sum:parseInt(sum)+parseInt(value)
            })
        }
    }
    incrementAsync=()=>{
        const {value} = this.selectNumbers
        const {sum} = this.state
        setTimeout(()=>{
            this.setState({
                sum:parseInt(sum)+parseInt(value)
            })
        },3000)
    }
    render() {
        const { sum } = this.state
        return (
            <div>
                <h1>当前求和为：{sum}</h1>
                <select ref={c => this.selectNumbers = c }>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                </select>&nbsp;
                <button onClick={this.increment}>+</button>&nbsp;
                <button onClick={this.decrement}>-</button>&nbsp;
                <button onClick={this.incrementifOdd}>当前求和为奇数+</button>&nbsp;
                <button onClick={this.incrementAsync}>异步+</button>
            </div>
        )
    }
}
